package com.service.sys;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import javax.servlet.http.HttpServletResponse;

import com.common.spring.SpringMvcUtils;
import com.dao.sys.SysDeptMapper;
import com.model.sys.SysDept;
import com.model.sys.SysUser;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.alibaba.excel.EasyExcel;
import com.common.StringUtils;
import com.common.easyui.EasyuiUtils;
import com.common.http.HttpResult;
import com.common.http.HttpStatus;
import com.dao.sys.SysMmDeptDao;
import com.model.sys.SysMmDeptModel;
import com.common.model.Global;

@Service
public class SysMmDeptService {
    static final Logger logger = LoggerFactory.getLogger(SysMmDeptService.class);

    @Autowired
    SysMmDeptDao mmDeptDao;
    @Autowired
    private SysDeptMapper sysDeptMapper;
    /**
     * 列表查询
     *
     * @param queryMap
     * @return
     */
    public HttpResult selectList(Map<String, Object> queryMap) {
        HttpResult httpResult = new HttpResult();
        EasyuiUtils.addPageNum(queryMap);
        EasyuiUtils.addFilter(queryMap);
        SysUser sysUser = SpringMvcUtils.getSysUser();
        if (!sysUser.getSuperadmin().equals(Global.YES + "")) {
            if (StringUtils.isNotNull(sysUser.getDatapermission())) {
                queryMap.put("deptIds", sysUser.getDatapermission().split(","));
            } else {
                queryMap.put("createId", sysUser.getUserid());
            }
        }
        List<SysMmDeptModel> list = mmDeptDao.selectList(queryMap);
        int count = mmDeptDao.selectListCount(queryMap);
        httpResult.setRows(list);
        httpResult.setTotal(count);
        logger.info("selectList:{}", list);
        return httpResult;
    }

    /**
     * 详情
     *
     * @param
     * @return
     */
    public HttpResult selectInfor(String id) {
        HttpResult httpResult = new HttpResult();
        SysMmDeptModel mmDeptModel = mmDeptDao.load(id);
        httpResult.setData(mmDeptModel);
        httpResult.setCode(HttpStatus.SUCCESS_CODE);
        return httpResult;
    }

    /**
     * 保存
     *
     * @param
     * @return
     */
    public HttpResult save(SysMmDeptModel mmDeptModel) {
        if (mmDeptModel == null) {
            return HttpResult.error("参数不能为空");
        }
        HttpResult httpResult = new HttpResult();
        int result = 0;
        String deptId = mmDeptModel.getDeptId();
        SysDept sysDept = queryDeptCodeByDeptId(deptId);
        if (null != sysDept) {
            mmDeptModel.setDeptCode(sysDept.getDwbm());
            mmDeptModel.setDeptName(sysDept.getDwmc());
        }
        if (!StringUtils.isNotNull(mmDeptModel.getId())) {
            mmDeptModel.setId(UUID.randomUUID().toString());
            result = mmDeptDao.insert(mmDeptModel);
        } else {
            result = mmDeptDao.update(mmDeptModel);
        }
        if (result > 0) {
            httpResult.setMsg(mmDeptModel.getStatus().equals(Global.YES + "") ? "提交成功!" : "保存成功!");
            httpResult.setData(mmDeptModel);
        } else {
            httpResult.setCode(HttpStatus.GLOBAL_EXCEPTION_CODE);
            httpResult.setMsg(mmDeptModel.getStatus().equals(Global.YES + "") ? "提交失败!" : "保存失败!");
        }
        return httpResult;
    }

    /**
     * 提交
     *
     * @param
     * @return
     */
    public HttpResult commit(SysMmDeptModel mmDeptModel) {
        if (mmDeptModel == null) {
            return HttpResult.error("参数不能为空");
        }
        mmDeptModel.setStatus(Global.YES + "");
        HttpResult httpResult = new HttpResult();
        int result = 0;
        result = mmDeptDao.update(mmDeptModel);
        if (result > 0) {
            httpResult.setMsg(mmDeptModel.getStatus().equals(Global.YES + "") ? "提交成功!" : "保存成功!");
            httpResult.setData(mmDeptModel);
        } else {
            httpResult.setCode(HttpStatus.GLOBAL_EXCEPTION_CODE);
            httpResult.setMsg(mmDeptModel.getStatus().equals(Global.YES + "") ? "提交失败!" : "保存失败!");
        }
        return httpResult;
    }

    /**
     * 删除
     *
     * @param
     * @return
     */
    public HttpResult delete(String id) {
        if (!StringUtils.isNotNull(id)) {
            return HttpResult.error("参数不能为空");
        }
        SysMmDeptModel model = mmDeptDao.load(id);
        if (!model.getStatus().equals(Global.NO + "")) {
            return HttpResult.error("非待提交状态，不可删除！");
        }
        HttpResult httpResult = new HttpResult();
        int result = mmDeptDao.delete(id);
        if (result > 0) {
            httpResult.setMsg("删除成功！");
        } else {
            httpResult.setCode(HttpStatus.GLOBAL_EXCEPTION_CODE);
            httpResult.setMsg("不存在此条数据！");
        }
        return httpResult;
    }

    /**
     * 撤回
     *
     * @param
     * @return
     */
    public HttpResult back(String id) {
        if (!StringUtils.isNotNull(id)) {
            return HttpResult.error("参数不能为空");
        }
        SysMmDeptModel model = mmDeptDao.load(id);

        //todo

        HttpResult httpResult = new HttpResult();
        model.setStatus(Global.NO + "");
        int result = mmDeptDao.update(model);
        if (result > 0) {
            httpResult.setMsg("撤回成功！");
        } else {
            httpResult.setCode(HttpStatus.GLOBAL_EXCEPTION_CODE);
            httpResult.setMsg("不存在此条数据！");
        }
        return httpResult;
    }

    /**
     * 导出
     *
     * @param queryMap
     * @param response
     */
    public void export(Map<String, Object> queryMap, HttpServletResponse response) {
        List<SysMmDeptModel> list = mmDeptDao.selectList(queryMap);
        logger.info("export:{}", list);
        try {
            EasyExcel.write(response.getOutputStream(), SysMmDeptModel.class).sheet("模板").doWrite(list);
        } catch (IOException e) {
            logger.error("导出错误:{}", e.getMessage());
        }

    }

    /**
     * deptId
     * 根据部门id获取机构编码
     *
     * @param deptId
     * @return
     */
    private SysDept queryDeptCodeByDeptId(String deptId) {
        SysDept sysDept = sysDeptMapper.selectByPrimaryKey(deptId);
        return sysDept;
    }
}
